package com.cskaoyan.config;

import com.alibaba.druid.pool.DruidDataSource;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @ConfigurationProperties注解的prefix属性值 + 成员变量名(set方法) = 配置文件中的key
 * @author stone
 * @date 2022/05/04 16:15
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "cskaoyan.datasource")
public class DataSourceConfiguration {

    //cskaoyan.datasource.driver-class-name
    String driverClassName; // = "com.mysql.jdbc.Driver";
    String url; // = "jdbc:mysql://localhost:3306/cskaoyan_db?useUnicode=true&characterEncoding=utf-8";
    String username; // = "root";
    String password; // = "123456";

    @Bean
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }
}
